package com.zkthink.ceres.cms.controller;

import static com.zkthink.ceres.common.constant.BizConstant.SUPER_TENANT;

import com.zkthink.base.R;
import com.zkthink.base.controller.SuperCacheController;
import com.zkthink.ceres.cms.dto.CmsTemplatePageDTO;
import com.zkthink.ceres.cms.dto.CmsTemplateSaveDTO;
import com.zkthink.ceres.cms.dto.CmsTemplateUpdateDTO;
import com.zkthink.ceres.cms.entity.CmsTemplate;
import com.zkthink.ceres.cms.service.CmsTemplateService;
import com.zkthink.dozer.DozerUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器 装修配置表
 * </p>
 *
 * @author kellen
 * @date 2020-05-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/cmsTemplate")
@Api(value = "CmsTemplate", tags = "装修配置")
//@PreAuth(replace = "cmsTemplate:")
@AllArgsConstructor
public class CmsTemplateController extends
    SuperCacheController<CmsTemplateService, Long, CmsTemplate, CmsTemplatePageDTO, CmsTemplateSaveDTO, CmsTemplateUpdateDTO> {

  private CmsTemplateService cmsTemplateService;

  private DozerUtils dozerUtils;

  @GetMapping("getPlatformTemplate")
  @ApiOperation(value = "查询平台装修配置", notes = "查询平台装修配置")
  public R<CmsTemplatePageDTO> getPlatformTemplate() {
    CmsTemplate cmsTemplate = cmsTemplateService.getCmsTemplate(SUPER_TENANT);
    CmsTemplatePageDTO cmsTemplatePageDTO = dozerUtils.map(cmsTemplate, CmsTemplatePageDTO.class);
    return R.success(cmsTemplatePageDTO);
  }

  @GetMapping("getMerchantTemplate")
  @ApiOperation(value = "查询商家装修配置", notes = "查询商家装修配置")
  public R<CmsTemplatePageDTO> getMerchantTemplate() {
    CmsTemplate cmsTemplate = cmsTemplateService.getCmsTemplate();
    CmsTemplatePageDTO cmsTemplatePageDTO = dozerUtils.map(cmsTemplate, CmsTemplatePageDTO.class);
    return R.success(cmsTemplatePageDTO);
  }

  @PostMapping("updateTemplate")
  @ApiOperation(value = "更新装修配置", notes = "更新装修配置")
  public R updateTemplate(@RequestBody CmsTemplateUpdateDTO cmsTemplateUpdateDTO) {
    cmsTemplateService.update(cmsTemplateUpdateDTO);
    return R.success();
  }

  @Override
  public R<CmsTemplate> handlerSave(CmsTemplateSaveDTO model) {
    //如果是非自定义模板，每个tenantCode只能有一个
    if (null == model.getIsCustom() || !model.getIsCustom()) {
      //查询是否有过配置
      boolean isHasAdd = cmsTemplateService.isHasAdd();
      if (isHasAdd) {
        return R.validFail("请勿重复添加默认模板");
      }
    }
    return R.successDef();
  }

  /**
   * Excel导入后的操作
   */
  @Override
  public R<Boolean> handlerImport(List<Map<String, String>> list) {
    List<CmsTemplate> cmsTemplateList = list.stream().map((map) -> {
      CmsTemplate cmsTemplate = CmsTemplate.builder().build();
      //TODO 请在这里完成转换
      return cmsTemplate;
    }).collect(Collectors.toList());

    return R.success(baseService.saveBatch(cmsTemplateList));
  }
}
